package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperation implements IOperation{

    private void delOperation(int pos,BookList bookList) {
        int currentSize = bookList.getUseSize();

        if(pos == currentSize-1) {
            bookList.setBook(pos,null);
            bookList.setUseSize(currentSize - 1);
            System.out.println("删除成功!");
            System.out.println();
            return;
        }
        for (int i = pos; i < currentSize - 1; i++) {
            bookList.setBook(i,bookList.getBook(i+1)) ;
        }
        bookList.setUseSize(currentSize - 1);
        System.out.println("删除成功!");
        System.out.println();

    }

    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书!");

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要删除的图书的名字:");
        String name = scanner.nextLine();

        int currentSize = bookList.getUseSize();
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)) {
                delOperation(i,bookList);
            }
        }
    }
}
